package com.bw.gmall.realtime.app.dwd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.utils.DateFormatUtil;
import com.bw.gmall.realtime.utils.MyKafkaUtil;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class BaseLogAppShopDwd {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        String topic="ods_traffic";
        String groupId="base_log_consumer";
        DataStreamSource<String> source = env.addSource(MyKafkaUtil.getFlinkKafkaConsumer(topic, groupId));
        SingleOutputStreamOperator<String> page = source.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                JSONObject jsonObject = JSON.parseObject(s);
                if (jsonObject.getJSONObject("page") != null) {
                    String string = jsonObject.getJSONObject("page").getString("page_action");
                    if (string.equals("a") || string.equals("b") || string.equals("c") || string.equals("e") || string.equals("f")) {
                        return true;
                    }
                    return false;
                } else {
                    return false;
                }

            }
        });
        SingleOutputStreamOperator<JSONObject> map = page.map(JSONObject::parseObject);
        //店铺id 需要两个key_by 浏览量缓存
        KeyedStream<JSONObject, String> keyedStream = map.keyBy(x -> x.getJSONObject("common").getString("mid"));
        keyedStream.process(new KeyedProcessFunction<String, JSONObject, JSONObject>() {
            ValueState<String> sw;
            ValueState<String> xw;
            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                ValueStateDescriptor<String> valueStateDescriptor = new ValueStateDescriptor<>("last_visit_dt", String.class);

                //配置状态生命周期
                valueStateDescriptor.enableTimeToLive(StateTtlConfig
                        .newBuilder(Time.days(1L)) // 过期时间1天
                        .setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
                        .build());
               this.sw= getRuntimeContext().getState(valueStateDescriptor);
               this.xw= getRuntimeContext().getState(valueStateDescriptor);

            }


            @Override
            public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, JSONObject>.Context context, Collector<JSONObject> collector) throws Exception {
                String value1 = sw.value();
                String value2 = xw.value();
                Long ts = jsonObject.getLong("ts");
                if(value1==null){
                    String s = DateFormatUtil.toYmdHms(ts);
                    Integer integer = new Integer(s.substring(11, 13));
                    if(integer<=12){
                        sw.update("1");
                        collector.collect(jsonObject);
                    }
                }
                if(value2==null){
                    String s = DateFormatUtil.toYmdHms(ts);
                    Integer integer = new Integer(s.substring(11, 13));
                    if(integer>12){
                        xw.update("1");
                        collector.collect(jsonObject);
                    }
                }
            }
        });
        page.print("------------->");
        page.addSink(MyKafkaUtil.getFlinkKafkaProducer("dwd_traffic"));
        env.execute();
    }
}
